﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Vit.Common;
using Vit.Web;
using Vit.Web.Models;
using Vit.Web.Providers.Data;

namespace Vit.AdminModule.Areas.Admin.Providers
{
    public class SqlLogDataProvider : DbContextDataProvider<WebProvidersDb, Log>
    {
        protected override IQueryable<Log> OrderByDefault(IQueryable<Log> query)
        {
            return query.OrderByDescending(o => o.LogTime);
        }

        public override object[] GetKeys(Log obj)
        {
            return new object[] { obj.ID };
        }

        public override Log GetByKeys(params object[] ids)
        {
            if (ids == null || ids.Length != 1)
                return null;
            string key = ids[0].ToString();
            return db.Logs.FirstOrDefault(o => o.ID == key);
        }

        public override Log SetDefaultValues(Log obj)
        {
            if (string.IsNullOrWhiteSpace(obj.ID))
                obj.ID = Guid.NewGuid().ToString();

            return obj;
        }
    }
}
